/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-02-10
 * Time: 19:10
 */
//泛型哈希桶的实现
public class HashBuck2<K,V> {
    private static class Node<K, V> {
        private K key;
        private V value;
        Node<K, V> next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }

    }
    public Node<K,V>[] array=(Node<K,V>[])new Node[10];
    public int size;
    public void put(K key, V value) {
        int hash = key.hashCode();
        int index = hash % array.length;//自己写引用类型的时候记得equals和hashcode方法
        Node cur = array[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                return;
            }
            cur = cur.next;
        }
        Node<K, V> node = new Node<>(key, value);
        node.next = array[index];
        array[index] = node;
        size++;
    }
        public V get(K key){
            int hash=key.hashCode();
            int index=hash%array.length;
          Node<K,V> cur =array[index];
            while(cur!=null){
                if(cur.key.equals(key)){
                    return cur.value;
                }
                cur=cur.next;
            }

            return null;
        }


}

